'use strict';
//店员列表
import React,{Component} from 'react';
import Header from '../../common/header';
import * as URLconstant from '../../constant/URLconstant'
import Util from '../../common/util';
import Toast from '../../common/ToastUtil';
import {
    View,
    Text,
    TouchableOpacity,
    Image,
    StyleSheet,
    ScrollView,
    Alert
} from 'react-native';
export default class desFans extends Component{
    // 构造
      constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            fansData:null,
            average:0,
            des_user_id:0
        };
      }

    componentDidMount() {
        this.getFansTotal()
    }

      getFansTotal(){
          let url = URLconstant.DELETE_WARNING
          let datakey=[]
          let del_fans = this.props.del_fans
          if(del_fans){
              datakey['del_user_id']=del_fans
          }
          datakey['user_id']=this.props.user_id
          Util.post(url,datakey,this.getTotalSuc.bind(this),this.errorCallback.bind(this))
      }

    getTotalSuc(datas){
          if(datas.status==0){
              this.setState({
                  fansData:datas.data
              })
          }
    }
    errorCallback(err){

    }

    submitDesFans(){
        let url = URLconstant.DES_FANS
        let datakey=[]
        datakey['user_id'] = this.props.user_id
        datakey['average'] = this.state.average
        let del_fans = this.props.del_fans
        if(del_fans){
            datakey['del_user_id']=del_fans
        }
        let des_user_id = this.state.des_user_id
        if(des_user_id!=0){
            datakey['n_work_id'] = des_user_id
        }
       Util.post(url,datakey,this.desFansSuc.bind(this),this.errorCallback.bind(this))
    }

    desFansSuc(datas){
          Toast.show(datas.msg)
        if(datas.status==0){
              this.props.reFresh()
              this.props.navigator.pop()
        }
    }

      render(){
          let fansData = this.state.fansData
          return(
              <View style={{flex:1,backgroundColor:"#f0f0f0"}}>
                  <Header navigator={this.props.navigator} initObj={{ title: '分配粉丝',id:2}}/>
                  {
                      fansData==null?
                          <View style={{flex:1}}>
                              {Util.loading()}
                          </View>:
                          <ScrollView style={{flex:1}}>
                              <View style={styles.des_header}>
                                  <Text style={{fontSize:14,color:"#A7A7A7"}}>团队详情</Text>
                                  <Text style={{fontSize:14,color:"#F75061",marginVertical:10}}>共有{fansData.info.fansCount}人</Text>
                                  <Text style={{fontSize:14,color:"#A7A7A7"}}>是否分配粉丝</Text>
                              </View>
                              <View style={styles.balance_view}>
                                <TouchableOpacity onPress={()=>this.setState({average:0})} style={styles.balance_item}>
                                    <Image
                                        source={this.state.average==0?require('../../imgs/selected.png'):require('../../imgs/select.png')}
                                        resizeMode={'cover'}
                                        style={{width:20,height:20,marginRight:10}}
                                    />
                                    <Text style={{fontSize:14,color:"#333"}}>不分配/不平均分配</Text>
                                </TouchableOpacity>
                                  <TouchableOpacity onPress={()=>this.setState({average:1,des_user_id:0})} style={[styles.balance_item,styles.border_b]}>
                                      <Image
                                          source={this.state.average==1?require('../../imgs/selected.png'):require('../../imgs/select.png')}
                                          resizeMode={'cover'}
                                          style={{width:20,height:20,marginRight:10}}
                                      />
                                      <Text style={{fontSize:14,color:"#333"}}>平均分配</Text>
                                  </TouchableOpacity>
                              </View>
                              {
                                  this.state.average==0?
                                  <View style={styles.des_header}>
                                      <Text style={{fontSize:14,color:"#A7A7A7"}}>分配给一下店员</Text>
                                  </View>:
                                      null
                              }
                              {
                                  this.state.average==0?
                                      <View style={styles.balance_view}>
                                          {
                                              fansData.info.otherShopMember.map((item,index)=>{
                                                  return(
                                                      <TouchableOpacity onPress={()=>this.setState({des_user_id:item.user_id})} style={index==0?styles.balance_item:[styles.balance_item,styles.border_b]} key={index}>
                                                          <Image
                                                              source={this.state.des_user_id==item.user_id?require('../../imgs/selected.png'):require('../../imgs/select.png')}
                                                              resizeMode={'cover'}
                                                              style={{width:20,height:20,marginRight:10}}
                                                          />
                                                          <Text style={{fontSize:14,color:"#333"}}>{item.real_name}</Text>
                                                      </TouchableOpacity>
                                                  )
                                              })
                                          }
                                      </View>
                                      :
                                      null
                              }

                          </ScrollView>
                  }
                  <TouchableOpacity onPress={()=>this.submitDesFans()} style={styles.confirm_btn}>
                      <Text style={{fontSize:16,color:"#fff"}}>确认分配</Text>
                  </TouchableOpacity>
              </View>
          )
      }
}
var styles = StyleSheet.create({
    des_header:{
        padding:10,
    },
    balance_view:{
        backgroundColor:'#fff',
        paddingHorizontal:10,
        marginBottom:10
    },
    balance_item:{
        height:45,
        flexDirection:'row',
        alignItems:'center'
    },
    border_b:{
        borderTopWidth:1,
        borderTopColor:"#e0e0e0"
    },
    confirm_btn:{
        width:Util.size.width,
        height:45,
        backgroundColor:"#f65061",
        alignItems:'center',
        justifyContent:'center'
    }
})